package com.kunda.netty.protobuf;

import java.util.Arrays;

import io.netty.channel.ChannelHandlerAdapter;
import io.netty.channel.ChannelHandlerContext;

public class SubReqServerHandler extends ChannelHandlerAdapter {

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg)
			throws Exception {
		SubscribeReqProto.SubscribeReq req = (SubscribeReqProto.SubscribeReq)msg;
		if("helloworld".equalsIgnoreCase(req.getUserName())){
			System.out.println("server accept client subscribe req:[" + req+"]");
			ctx.writeAndFlush(resp(req.getSubReqId()));
		}
	}
	
	private SubscribeRespProto.SubscribeResp resp(int reqId){
		//SubscribeRespProto.SubscribeResp resp = new SubscribeRespProto.SubscribeResp();
		SubscribeRespProto.SubscribeResp.Builder builder = SubscribeRespProto.SubscribeResp.newBuilder();
		
		builder.setSubReqId(reqId);
		builder.setRespCode(0);
		builder.addAllDesc(Arrays.asList(new String[]{"Netty book order success"}));
		return builder.build();
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
			throws Exception {
		cause.printStackTrace();
		ctx.close();
	}
	
	
}
